package com.huijie.app.util;

import com.huijie.app.u.entity.Token;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ContextHelper
 * @Description 上下文工具
 * @Author zyc
 * @Date 2019/1/8 16:05
 * @Version 1.0
 */
public class ContextHelper {

    private static Log log = LogFactory.getLog(ContextHelper.class);

    /**
     * @param
     * @return java.lang.String
     * @description: 同一个线程下，获取用户ID
     * @author zyc
     * @date 2019/1/10 9:55
     */
    public static HttpServletRequest getCurrRequest(){
        HttpServletRequest request = null;
        try {
            request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        }catch (Exception e){
//            log.warn("错误调用上下文工具，无request!",e);
        }
        return request;
    }

    /**
     * @param request
     * @return com.huijie.app.u.entity.Token
     * @description: 根据request获取token
     * @author zyc
     * @date 2019/1/10 17:24
     */
    public static Token getTokenByRequest(HttpServletRequest request){
        Token token = null;
        try {
            token = TokenUtil.getToken(request);
        }catch (Exception e){
            log.error(e);
        }
        return token;
    }


    /**
     * @param
     * @return java.lang.String
     * @description: 同一个线程下，获取用户ID
     * @author zyc
     * @date 2019/1/10 9:55
     */
    public static Integer getCurrUserId(){
        Integer res = null;
        Token token = getTokenByCurrRequest();
        if(token!=null){
            res = token.getUserId();
        }
        return res;
    }

    /**
     * @param
     * @return java.lang.String
     * @description: 同一个线程下，获取用户登录名
     * @author zyc
     * @date 2019/1/10 9:55
     */
    public static String getCurrUserLoginName(){
        String res = "";
        Token token = getTokenByCurrRequest();
        if(token!=null){
            res = token.getUserName();
        }
        return res;
    }

    /**
     * @param request
     * @return java.lang.String
     * @description: 不同线程下，获取用户ID
     * @author zyc
     * @date 2019/1/10 9:55
     */
    public static Integer getUserIdByRequest(HttpServletRequest request){
        Integer res = null;
        Token token = getTokenByRequest(request);
        if(token!=null){
            res = token.getUserId();
        }
        return res;
    }

    /**
     * @param request
     * @return java.lang.String
     * @description: 不同线程下，获取用户登录名
     * @author zyc
     * @date 2019/1/10 9:55
     */
    public static String getUserLoginNameByRequest(HttpServletRequest request){
        String res = "";
        Token token = getTokenByRequest(request);
        if(token!=null){
            res = token.getUserName();
        }
        return res;
    }

    /**
     * @param
     * @return com.huijie.app.u.entity.Token
     * @description: 根据当前request获取token
     * @author zyc
     * @date 2019/1/10 17:24
     */
    public static Token getTokenByCurrRequest(){
        Token token = null;
        HttpServletRequest request = getCurrRequest();;
        try {
            request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        }catch (Exception e){
            log.warn("错误调用上下文工具，无request!",e);
        }
        if(request!=null){
            token = getTokenByRequest(request);
        }
        return token;
    }

}
